﻿#include "WRWorkServicesGuiPart.h"
#include "WRWorkServicesGuiView.h"
#include "../WRSoft-Utility/Runtime/WRClass.h"
#include "../WRSoft-GuiApp-Working-Common/Runtime/WRWorkMainGuiPartActivator.h"

#include <QTabWidget>
#include <QHBoxLayout>

//表示工作服务管理部件创建器。
class WRWorkServicesGuiPartActivator : public WRWorkMainGuiPartActivator
{
public:
    //初始化工作服务管理部件创建器。
    inline WRWorkServicesGuiPartActivator(void)
    {
        QChar className[] = WRWorkServicesGuiPartStrArr;
        WRClass::enroll(WRWorkServicesGuiPartClass, QString(className, WRWorkServicesGuiPartStrLen), this);
    }
    //释放当前对象所占用的相关资源。
    ~WRWorkServicesGuiPartActivator(void)
    {
        //add code here
    }

    //创建工作服务管理部件。
    WRWorkMainGuiPart* create(const QDomElement&)
    {
        return new WRWorkServicesGuiPart();
    }
};
//唯一实例。
static WRWorkServicesGuiPartActivator g_workServicesGuiPartActivator;

//初始化界面组件。
WRWorkServicesGuiPart::WRWorkServicesGuiPart(void)
{
    m_widget = 0;
    m_current = -1;
}
//释放当前对象所占用的相关资源。
WRWorkServicesGuiPart::~WRWorkServicesGuiPart(void)
{
    //add code here
}

//初始部件。
void WRWorkServicesGuiPart::init(const WRObjRef<WRWorkDaemon>& daemon)
{
    QTabWidget* tabWidget = new QTabWidget();
    {
        tabWidget->addTab(new WRWorkServicesGuiView(daemon), QStringLiteral("{local}"));

        const WRArray<WRWorkDomain*>& domains = daemon->domains();
        for(WRu i = 0, l = domains.count(); i < l; ++i)
        {
            WRWorkDomain* domain = domains[i];
            tabWidget->addTab(new WRWorkServicesGuiView(domain), domain->title());
        }

        m_current = 0;
        tabWidget->setCurrentIndex(0);

        QObject::connect(
                    tabWidget, SIGNAL(currentChanged(int)),
                    this, SLOT(onCurrentChanged(int)));
    }
    m_widget = tabWidget;

    QHBoxLayout* layout = new QHBoxLayout();
    layout->setContentsMargins(2, 2, 2, 2);
    layout->addWidget(m_widget);
    setLayout(layout);
}

//打开部件。
void WRWorkServicesGuiPart::open(void)
{
    if (m_current != -1)
    {
        QTabWidget* widget = qobject_cast<QTabWidget*>(m_widget);
        //打开当前标签页
        WRWorkServicesGuiView* guiView = qobject_cast<WRWorkServicesGuiView*>(widget->widget(m_current));
        guiView->open();
    }
}
//关闭部件。
void WRWorkServicesGuiPart::close(void)
{
    if (m_current != -1)
    {
        QTabWidget* widget = qobject_cast<QTabWidget*>(m_widget);
        //关闭当前标签页
        WRWorkServicesGuiView* guiView = qobject_cast<WRWorkServicesGuiView*>(widget->widget(m_current));
        guiView->close();
    }
}

//当标签页改变后引发此事件。
void WRWorkServicesGuiPart::onCurrentChanged(int index)
{
    QTabWidget* widget = qobject_cast<QTabWidget*>(m_widget);
    //关闭上次标签页
    WRWorkServicesGuiView* guiView = qobject_cast<WRWorkServicesGuiView*>(widget->widget(m_current));
    guiView->close();
    //保存当前标签面
    m_current = index;
    //打开当前标签页
    guiView = qobject_cast<WRWorkServicesGuiView*>(widget->widget(m_current));
    guiView->open();
}

//获取推荐尺寸。
QSize WRWorkServicesGuiPart::sizeHint() const
{
    return QSize(800, 600);
}
